var bossTimeData = {
	data: {
		bossTimeList: {},
		nowTime: new Date()
	},
	mounted: function() {
		var T = this;
		T.getBossTimeData();
		setInterval(function() {
			T.getBossTimeData();
		}, 2000);
	},
	methods: {
		/**
		 * 获取最近一个时间
		 * @param {Object} timeList
		 */
		getNearestTime: function(timeList) {
			var now = new Date();
			var h = now.getHours();
			var m = now.getMinutes();
			var s = now.getSeconds();
			var nowTimeStr = h + ":" + m + ":" + s;
			var nowSeconds = formatTimeToSeconds(nowTimeStr);
			var nextTime = "";
			var prevTime = "";
			for(var i = 0; i < timeList.length; i++) {
				var bossTimeSeconds = formatTimeToSeconds(timeList[i]);
				var delta = bossTimeSeconds - nowSeconds;
				if(delta >= 0) {
					nextTime = timeList[i];
					prevTime = timeList[i - 1];
					break;
				}
			}
			return {
				nextTime:nextTime,
				prevTime:prevTime
			};
		},
		getBossTimeData: function() {
			var T = this;
			mui.ajax({
				url: "data/bosstime.json",
				type: "GET",
				dataType: "json",
				success: function(result) {
					var bList = [];
					for(k in result) {
						var timeObj = T.getNearestTime(result[k].time, "next");
						result[k].timeObj = timeObj;
						bList.push(result[k]);
					}
					//按照nextTime排序之后显示
					T.bossTimeList = _.sortBy(bList, function(obj) {
						return obj.timeObj.nextTime;
					});
				}
			});
		}
	}
};

function formatSeconds(value) {
	var theTime = parseInt(value);
	var theTime1 = 0;
	var theTime2 = 0;

	if(theTime >= 60) {
		theTime1 = parseInt(theTime / 60);
		theTime = parseInt(theTime % 60);
		if(theTime1 >= 60) {
			theTime2 = parseInt(theTime1 / 60);
			theTime1 = parseInt(theTime1 % 60);
		}
	}
	if(theTime < 10) {
		theTime = "0" + parseInt(theTime);
	}
	var result = "" + theTime + "";
	if(theTime1 >= 0) {
		if(theTime1 < 10) {
			theTime1 = "0" + parseInt(theTime1);
		}
		result = "" + theTime1 + ":" + result;
	}
	if(theTime2 >= 0) {
		if(theTime2 < 10) {
			theTime2 = "0" + parseInt(theTime2);
		}
		result = "" + theTime2 + ":" + result;
	}
	return result;
}

function formatTimeToSeconds(v_time) {
	var t_arr = v_time.split(":");
	var hh = t_arr[0];
	var mm = t_arr[1];
	var ss = t_arr[2];
	var s_now = parseInt(hh * 3600) + parseInt(mm * 60) + parseInt(ss);
	return s_now;
};